package com.zx.map.controller;

import com.zx.map.service.BillCheckLogService;
import com.zx.map.service.OrdLogService;
import com.zx.map.service.domain.BillCheckLogDomain;
import com.zx.map.service.model.BillCheckLogModel;
import com.zx.map.service.model.OrdLogModel;
import com.zx.map.service.model.PaymentLogModel;
import com.zx.map.service.model.SettleLogModel;
import com.zx.platform.utils.PmpUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 *	商户/代理结算信息
 * @author wenjw
 *
 */
@Controller
@RequestMapping("/manage/billCheckLog")
public class BillCheckLogController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(BillCheckLogController.class);

	@Autowired
	private BillCheckLogService billCheckLogService;
	

	
	@Autowired
	private OrdLogService ordLogService;

	
	/**
	 * 进入列表页面
	 * 当日对账查询
	 * @param domain
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/todayQuery")
	public String todayQuery(BillCheckLogDomain domain, Model model,
			HttpServletRequest request) {

		try {// 初始化信息
			Integer pageNum = domain.getPageNum();
			Integer pageSize = domain.getPageSize();
			Integer pageCount = domain.getPageCount();
			String yesterday = PmpUtils.dateToString(PmpUtils.addDate(new Date(), -1),"yyyyMMdd");
			domain.setBillDate(yesterday);
			if (null == pageNum) {
				pageNum = 1;
			}
			if (null == pageSize) {
				pageSize = 10;
			}

			Map map = billCheckLogService.getPageByDomain(pageSize, pageNum, domain);
			List<BillCheckLogModel> billCheckLogModelList = (List<BillCheckLogModel>) map.get("list");
			int recordCount = Integer.valueOf(map.get("count").toString());
			// 页面信息
			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", billCheckLogModelList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", domain);
			return "/jsp/map/billCheckLog/todayQuery";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}
	/**
	 * 进入列表页面
	 * 历史对账查询
	 * @param domain
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/list")
	public String list(BillCheckLogDomain domain, Model model,
			HttpServletRequest request) {

		try {// 初始化信息
			Integer pageNum = domain.getPageNum();
			Integer pageSize = domain.getPageSize();
			Integer pageCount = domain.getPageCount();
			if (null == pageNum) {
				pageNum = 1;
			}
			if (null == pageSize) {
				pageSize = 10;
			}

			Map map = billCheckLogService.getPageByDomain(pageSize, pageNum, domain);
			List<BillCheckLogModel> menuModelList = (List<BillCheckLogModel>) map.get("list");
			int recordCount = Integer.valueOf(map.get("count").toString());
			// 页面信息
			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", menuModelList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", domain);
			return "/jsp/map/billCheckLog/billCheckLogList";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}


	
    /**  
     * 表单提交日期绑定  
     */  
    @InitBinder  
    public void initBinder(WebDataBinder binder) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    }
    /**
     * 弹出的新增修改页面
     * @param billCheckLogModel
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/edit")
    public String edit(BillCheckLogModel billCheckLogModel, Model model, HttpServletRequest request) {
    	try{

            Integer settleId = billCheckLogModel.getBillId();
    		if(settleId!=-1&&settleId!=null){
    			billCheckLogModel=billCheckLogService.getModelByKey(settleId);
    		}
	    	model.addAttribute("model", billCheckLogModel);
	        return "/jsp/map/billCheckLog/billCheckLogEdit";
	    } catch (Exception e) {
	        log.error("内部异常", e);
	        model.addAttribute("errorMsg", "出错了");
	        return "/jsp/platform/error";
	    }
    }
    /**
     * @return
     */
    @RequestMapping("/update")
    public
    @ResponseBody
    BillCheckLogModel update(BillCheckLogModel model) {
        try {
        	billCheckLogService.update(model);
        	model.setSuccess(true);
        	model.setMsg("保存信息成功");
            log.debug("保存信息成功");
        } catch (Exception e) {
        	model.setSuccess(false);
        	model.setMsg("保存信息失败");
            log.error("保存信息失败" + e.getMessage(), e);
        }
        return model;
    }

    /**
     * @return
     */
    @RequestMapping("/delete")
    public
    @ResponseBody
    BillCheckLogModel delete(BillCheckLogModel model) {
        try {
        	billCheckLogService.delete(model);
        	model.setSuccess(true);
        	model.setMsg("删除信息成功");
            log.debug("删除信息成功");
        } catch (Exception e) {
        	model.setSuccess(false);
        	model.setMsg("删除信息失败");
            log.error("删除信息失败" + e.getMessage(), e);
        }
        return model;
    }

    /**
     * @return
     */
    @RequestMapping("/add")
    public
    @ResponseBody
    BillCheckLogModel add(BillCheckLogModel model) {
        try {
        	billCheckLogService.add(model);
        	model.setSuccess(true);
        	model.setMsg("新增信息成功");
            log.debug("新增信息成功");
        } catch (Exception e) {
        	model.setSuccess(false);
        	model.setMsg("新增信息失败");
            log.error("新增信息失败" + e.getMessage(), e);
        }
        return model;
    }
}
